import { ItemData } from "./ItemData";

export class InventoryItem {

    itemData: ItemData

    amount: number

}

export class InventoryData {

    items: Array<InventoryItem> = new Array()

    public AddItem(itemData: ItemData, amount: number) {
        // 判断背包中是否存在可堆叠的
        let found: boolean = false;
        if (itemData.stackable) {
            let idx = this.items.findIndex(item => item.itemData == itemData)
            if (idx >= 0) {
                this.items[idx].amount += amount
                found = true;
            }
        }
        for(let i = 0 ; i < this.items.length ; i++){
            if(this.items[i].itemData == null && !found){
                this.items[i].itemData = itemData
                this.items[i].amount = amount
            }
        }
    }

}